package com.wep.fence.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.wep.fence.dto.RegionDTO;
import com.wep.fence.entity.RegionEntity;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author wep
 * @version 1.0
 * @description:
 * @date 2025/4/25 18:27
 */
@Repository
@Mapper
public interface RegionMapper extends BaseMapper<RegionEntity> {

    @Select("WITH RECURSIVE AreaTree AS ( " +
            "SELECT *, type AS level " +
            "FROM sys_region WHERE CODE=#{code} " +
            "UNION ALL " +
            "SELECT child.*, parent.level + 1 " +
            "FROM sys_region child " +
            "INNER JOIN AreaTree parent ON child.pcode=parent.code " +
            "WHERE parent.level < #{type} ) " +
            "SELECT * FROM AreaTree WHERE level <= #{type}")
    List<RegionDTO> findAllDescendantsByCode(String code, Integer type);
}
